class HealthViewer : StaticEventHandler {
	Gc_ShowFriendHp	m_show_hp;
	transient Cvar	cvar_show;
	
	override void OnRegister () {
		m_show_hp = new('Gc_ShowFriendHp');
	}
	
	override void PlayerEntered (PlayerEvent event) {
		cvar_show = Cvar.GetCvar("gc_viewhealth", players [consoleplayer]);
		
		if(consoleplayer == event.playernumber) {
			m_show_hp.Init(players [consoleplayer].mo);
		}
	}
	
	/* override void WorldLoaded (WorldEvent event) {
		cvar_show = Cvar.GetCvar("gc_viewhealth", players [consoleplayer]);
		
		if(event.issavegame && players [consoleplayer].mo) {
			m_show_hp.Init(players [consoleplayer].mo);
		}
	} */
	
	override void WorldTick () {
		m_show_hp.FindFriendEvent();
		m_show_hp.OutDrawData();
	}
	
	override void RenderOverlay (RenderEvent event) {
		if(cvar_show && cvar_show.GetBool()) {
			m_show_hp.Draw(event);
		}
	}
}

/*	kd:
	Handles finding friendly targets to output their health on the screen. It
	also does things like fading and knows when targets are merely switched,
	just found or just left.
	
	Note it's a class just because I wanted it to contain the draw function
	and the event handler above is like, "you can't do this from a struct!" */

class Gc_ShowFriendHp play {
	const max_alpha   = 0.8;
	const fade_tics   = 7;
	const fade_rate   = max_alpha / fade_tics;
	const conditions  = 4;
	const offset_x    = 0.5;
	const offset_y    = 0.0666;
	const font_name   = "smallfont";
	
	enum fading_state {
		FADE_IN,
		FADE_OUT,
		FADE_STAY
	}
	
	float          m_old_alpha;
	float          m_alpha;
	fading_state   m_state;
	Actor          m_user;
	Actor          m_target;
	bool           m_had_target;
	
	Actor   m_shown_target;
	int     m_health;
	int     m_max_health;
	int     m_cond_color;
	String  m_name;
	String  m_percent;
	
	// Some data we need about the window to allow scaling.
	// Font    m_font;
	float   m_scale;
	float   m_canvas_w;
	float   m_canvas_h;
	float   m_draw_x;
	float   m_draw_y;
	// float   m_line_offset;
	
	// Justification offsets so the text appears centred.
	float   m_name_just;
	float   m_percent_just;
	
	void Init (Actor user) {
		m_user    = user;
		m_alpha   = 0;
		m_state   = FADE_STAY;
		// m_font    = Font.GetFont(font_name);
		ReinitScreen();
	}
	
	void ReinitScreen () {
		float scale = 1;
		
		if(m_user && m_user.player) {
			Cvar hud_scale = Cvar.GetCvar('hud_scale', m_user.player);
			scale = max(1, hud_scale.GetFloat());
		}
		
		m_canvas_w  = Screen.GetWidth()   / scale;
		m_canvas_h  = Screen.GetHeight()  / scale;
		m_draw_x    = m_canvas_w   * offset_x;
		m_draw_y    = m_canvas_h   * offset_y;
		
		/* if(m_font) {
			m_line_offset = m_font.GetHeight();
		}
		
		else {
			Console.printf("Gc_ShowFriendHp: I have no font! Tried %s.",
				font_name);
		} */
	}
	
	void FindFriendEvent () {
		// Safe is safe.
		if(!m_user) {
			return;
		}
		
		Actor target = m_user.AimTarget();
		
		if(target /* && target.IsFriend(m_user) */ && 0 < target.health) {
			// Just started aiming at a target.
			if(m_target == NULL) {
				// We could go and organise this into function calls, but iz's
				// not really necessary to improve reading this.
				
				// m_name = PlayerPawn.GetPrintableDisplayName(target);
				
				if(target.CheckClass('PlayerPawn', AAPTR_DEFAULT, true)) {
					m_name = target.player.GetUserName();
				}
				
				else {
					m_name = target.GetTag();
				}
				
				m_state = FADE_IN;
			}
			
			// Just went from one target to another.
			else if(target != m_target) {
				// m_name = PlayerPawn.GetPrintableDisplayName(target);
				if(target.CheckClass('PlayerPawn', AAPTR_DEFAULT, true)) {
					m_name = target.player.GetUserName();
				}
				
				else {
					m_name = target.GetTag();
				}
			}
			
			m_target = target;
			m_shown_target = target;
			m_had_target = true;
		}
		
		// Just stopped aiming at any targets.
		else if(m_had_target) {
			m_state = FADE_OUT;
			m_target = NULL;
			m_had_target = false;
		}
	}
	
	void OutDrawData () {
		// I don't get zs sometimes.
		static const int cond_colors [] = {
			Font.CR_RED,
			Font.CR_ORANGE,
			Font.CR_YELLOW,
			Font.CR_GREEN
		};
		
		m_old_alpha    = m_alpha;
		
		if(m_shown_target) {
			m_health       = m_shown_target.health;
			m_max_health   = m_shown_target.SpawnHealth();
		}
		
		m_max_health = max(1, m_max_health);
		
		m_percent = String.Format("%01.1f%%",
			max(0, 100.0 * m_health / m_max_health));
		
		int cond_i = clamp(
			conditions * m_health / m_max_health,
			0, conditions - 1);
		
		m_cond_color = cond_colors [cond_i];
		
		// Justification offsets.
		Font text_font = Font.GetFont(font_name);
		m_name_just     = 0.5 * text_font.StringWidth(m_name);
		m_percent_just  = 0.5 * text_font.StringWidth(m_percent);
		
		// Handle fading in and out.
		switch(m_state) {
		case FADE_IN:
			m_alpha += fade_rate;
			
			if(max_alpha <= m_alpha) {
				m_state = FADE_STAY;
				m_alpha = max_alpha;
			}
			
			break;
		case FADE_OUT:
			m_alpha -= fade_rate;
			
			if(m_alpha <= 0) {
				m_state = FADE_STAY;
				m_alpha = 0;
			}
			
			break;
		}
	}
	
	ui void Draw (RenderEvent event) {
		float alpha =	m_alpha     * event.fractic +
						m_old_alpha * (1 - event.fractic);
		
		Font text_font = Font.GetFont(font_name);
		
		Screen.DrawText(text_font, Font.CR_UNTRANSLATED,
			m_draw_x - m_name_just,
			m_draw_y,
			m_name,
			DTA_VIRTUALWIDTHF, m_canvas_w,
			DTA_VIRTUALHEIGHTF, m_canvas_h,
			DTA_ALPHA, m_alpha,
			DTA_KEEPRATIO, true);
		Screen.DrawText(text_font, m_cond_color,
			m_draw_x - m_percent_just,
			m_draw_y + text_font.GetHeight(),
			m_percent,
			DTA_VIRTUALWIDTHF, m_canvas_w,
			DTA_VIRTUALHEIGHTF, m_canvas_h,
			DTA_ALPHA, m_alpha,
			DTA_KEEPRATIO, true);
	}
}